// task_create_fill_in_page.dart
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:provider/provider.dart';
import '../../../constants.dart';
import '../../../repository/model/task_select_options_model.dart';
import '../../../route/route_utils.dart';
import '../../../route/routes.dart';
import 'model/task_step_model.dart';
import 'task_create_fill_in_vm.dart';

class TaskCreateFillInPage extends StatefulWidget {
  final String? taskId;
  final String taskCategoryStr;
  final double? serviceFeePercent;
  final bool againCreate;
  final VoidCallback? refreshHandle;

  const TaskCreateFillInPage({
    super.key,
    this.taskId,
    required this.taskCategoryStr,
    this.serviceFeePercent,
    this.againCreate = false,
    this.refreshHandle,
  });

  @override
  _TaskCreateFillInPageState createState() => _TaskCreateFillInPageState();
}

class _TaskCreateFillInPageState extends State<TaskCreateFillInPage> {
  late final TaskCreateFillInViewModel _viewModel;
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _explainController = TextEditingController();
  final TextEditingController _priceController = TextEditingController();
  final TextEditingController _countController = TextEditingController();
  late String _currentTaskCategoryStr;

  @override
  void initState() {
    super.initState();
    _currentTaskCategoryStr = widget.taskCategoryStr;
    _viewModel = TaskCreateFillInViewModel(
      taskCategoryStr: widget.taskCategoryStr,
      taskId: widget.taskId,
    );
    _viewModel.setAgainCreate(widget.againCreate);
    _viewModel.requestContent();
  }

  // 从 ViewModel 更新控制器值
  void _updateControllersFromViewModel(TaskCreateFillInViewModel viewModel) {
    if (viewModel.taskTitleStr != null && _titleController.text.isEmpty) {
      _titleController.text = viewModel.taskTitleStr!;
    }

    if (viewModel.taskExplain != null && _explainController.text.isEmpty) {
      _explainController.text = viewModel.taskExplain!;
    }

    // 如果是编辑模式，设置价格和次数
    if (widget.taskId != null) {
      // 这里需要从 ViewModel 中获取实际的价格和次数值
      // 暂时使用空值，需要根据实际数据结构调整
      if (_priceController.text.isEmpty) {
        _priceController.text = viewModel.taskPrice ?? '';
      }
      if (_countController.text.isEmpty) {
        _countController.text = viewModel.taskCount ?? '';
      }
    }
  }


  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: _viewModel,
      child: Consumer<TaskCreateFillInViewModel>(
        builder: (context, viewModel, child) {
          // 在构建时检查并更新控制器值
          WidgetsBinding.instance.addPostFrameCallback((_) {
            _updateControllersFromViewModel(viewModel);
          });
          return Scaffold(
            appBar: AppBar(
              title: Text(
                widget.taskId != null && !widget.againCreate ? '修改任务' : '发布任务',
              ),
              leading: IconButton(
                icon: const Icon(Icons.arrow_back),
                onPressed: () => _onBackPressed(viewModel),
              ),
            ),
            body: _buildBody(viewModel),
            bottomNavigationBar: _buildBottomBar(viewModel),
          );
        },
      ),
    );
  }

  Widget _buildBody(TaskCreateFillInViewModel viewModel) {
    if (viewModel.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    return SingleChildScrollView(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 任务类型选择
          _buildTaskTypeSection(),
          const SizedBox(height: 16),

          // 任务标题
          _buildTaskTitleSection(),
          const SizedBox(height: 16),

          // 任务说明
          _buildTaskDescriptionSection(viewModel),
          const SizedBox(height: 16),

          // 任务步骤
          _buildTaskStepsSection(viewModel),
          const SizedBox(height: 16),

          // 任务要求
          _buildTaskRequirementsSection(viewModel),
          const SizedBox(height: 16),

          // 任务奖励
          _buildTaskRewardSection(viewModel),
          const SizedBox(height: 16),

          // 其他设置
          _buildOtherSettingsSection(viewModel),
        ],
      ),
    );
  }

  Widget _buildTaskTypeSection() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: GestureDetector(
        onTap: _selectTaskType,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  '任务类型（必选）',
                  style: TextStyle(fontSize: 14, color: Color(0xFF333333)),
                ),
                const SizedBox(height: 8),
                const Text(
                  '请正确选择任务类型，否则会被拒审',
                  style: TextStyle(fontSize: 12, color: Color(0xFF999999)),
                ),
              ],
            ),
            Container(
              child: Row(
                children: [
                  Text(
                    _currentTaskCategoryStr,
                    style: const TextStyle(
                      fontSize: 14,
                      color: Color(0xFF2D2D2D),
                    ),
                  ),
                  const SizedBox(width: 4),
                  Image.asset(
                    'assets/images/renwu_more_icon.png',
                    width: 7.h,
                    height: 11.h,
                    fit: BoxFit.cover,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTaskTitleSection() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '任务标题（必填）',
            style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
          ),
          const SizedBox(height: 8),
          TextField(
            controller: _titleController,
            decoration: InputDecoration(
              hintText: '建议格式：【任务名称】+目的',
              hintStyle: const TextStyle(
                fontSize: 14,
                color: Color(0xFF999999),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(4),
                borderSide: const BorderSide(
                  color: Color(0xFFDDDDDD),
                ), // 这里设置颜色
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(4),
                borderSide: const BorderSide(color: Colors.blue), // 聚焦时的颜色
              ),
              contentPadding: const EdgeInsets.all(12),
              filled: true,
              fillColor: Colors.white,
            ),
            onChanged: (value) => _viewModel.setTitleChanged(value),
          ),
        ],
      ),
    );
  }

  Widget _buildTaskDescriptionSection(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '任务说明（必填）',
            style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
          ),
          const SizedBox(height: 8),
          TextField(
            controller: _explainController,
            maxLines: 3,
            minLines: 3,
            decoration: InputDecoration(
              hintText: '请简要描述任务',
              hintStyle: const TextStyle(
                fontSize: 14,
                color: Color(0xFF999999),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(4),
                borderSide: const BorderSide(
                  color: Color(0xFFDDDDDD),
                ), // 这里设置颜色
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(4),
                borderSide: const BorderSide(color: Colors.blue), // 聚焦时的颜色
              ),
              contentPadding: const EdgeInsets.all(12),
              filled: true,
              fillColor: Colors.white,
            ),
            onChanged: (value) => _viewModel.setExplainChanged(value),
          ),
        ],
      ),
    );
  }

  Widget _buildTaskStepsSection(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '任务步骤（必填，最多5步）',
            style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
          ),
          const SizedBox(height: 8),

          // 显示已有步骤
          if (viewModel.taskSteps.isNotEmpty) ...[
            ...viewModel.taskSteps.map(
                  (step) => _buildStepItem(step, viewModel),
            ),
            const SizedBox(height: 8),
          ],

          // 添加步骤按钮
          if (viewModel.taskSteps.length < 5)
            SizedBox(
              width: double.infinity,
              child: OutlinedButton(
                onPressed: _addTaskStep,
                style: OutlinedButton.styleFrom(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(4),
                  ),
                  side: const BorderSide(color: Color(0xFFDDDDDD)),
                ),
                child: const Text(
                  '+ 添加任务步骤',
                  style: TextStyle(fontSize: 14, color: Color(0xFF333333)),
                ),
              ),
            ),
        ],
      ),
    );
  }

  Widget _buildStepItem(
      TaskStepModel step,
      TaskCreateFillInViewModel viewModel,
      ) {
    return Container(
      margin: const EdgeInsets.only(bottom: 8),
      padding: const EdgeInsets.only(left: 12, right: 12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(4),
        border: Border.all(color: const Color(0xFFDDDDDD)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 步骤标题和操作按钮
          Row(
            children: [
              Expanded(
                child: Text(
                  '步骤${step.sortValue! + 1}',
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),
                ),
              ),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  IconButton(
                    icon: const Icon(
                      Icons.edit,
                      size: 16,
                      color: Color(0xFF999999),
                    ),
                    onPressed: () => _editStep(step),
                  ),
                  IconButton(
                    icon: const Icon(
                      Icons.delete,
                      size: 16,
                      color: Color(0xFF999999),
                    ),
                    onPressed: () => _deleteStep(step, viewModel),
                  ),
                ],
              ),
            ],
          ),
          // 根据步骤类型显示不同内容
          _buildStepContent(step),
        ],
      ),
    );
  }

  // 根据步骤类型构建不同的内容显示
  Widget _buildStepContent(TaskStepModel step) {
    switch (step.taskStepType) {
      case 'CONTENT':
        return _buildContentStep(step);
      case 'LINK':
        return _buildLinkStep(step);
      case 'INFO':
        return _buildInfoStep(step);
      default:
        return _buildDefaultStep(step);
    }
  }

  // 图文内容步骤
  Widget _buildContentStep(TaskStepModel step) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 文字描述
        if (step.taskDescription.isNotEmpty)
          Text(
            step.taskDescription,
            maxLines: 5,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(fontSize: 12, color: Color(0xFF666666)),
          ),
        SizedBox(height: 8.h),
        // 图片预览（如果有）
        if (step.taskStepImages != null && step.taskStepImages!.isNotEmpty)
          SizedBox(
            height: 96.h,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: step.taskStepImages!.length,
              padding: const EdgeInsets.only(bottom: 8),
              itemBuilder: (context, imgIndex) {
                return GestureDetector(
                  onTap: () {
                    _showImagePreview(context, step.taskStepImages!, imgIndex);
                  },
                  child: Padding(
                    padding: EdgeInsets.only(top: 8.h, right: 8.w),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(8.r),
                      child: CachedNetworkImage(
                        imageUrl: step.taskStepImages![imgIndex],
                        height: 80.h,
                        width: 80.h,
                        fit: BoxFit.cover,
                        errorWidget: (context, url, error) {
                          return Image.asset(
                            'assets/images/error_image_load.png',
                            width: 80.h,
                            height: 80.h,
                            fit: BoxFit.cover,
                          );
                        },
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
      ],
    );
  }

  void _showImagePreview(
      BuildContext context,
      List<String> images,
      int initialIndex,
      ) {
    showDialog(
      context: context,
      builder: (context) => Dialog(
        backgroundColor: Colors.transparent,
        insetPadding: EdgeInsets.zero,
        child: SizedBox(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height * 0.8,
          child: PhotoViewGallery.builder(
            itemCount: images.length,
            builder: (context, index) {
              return PhotoViewGalleryPageOptions(
                imageProvider: CachedNetworkImageProvider(images[index]),
                minScale: PhotoViewComputedScale.contained,
                maxScale: PhotoViewComputedScale.covered * 2,
                initialScale: PhotoViewComputedScale.contained,
              );
            },
            backgroundDecoration: const BoxDecoration(color: Colors.black),
            pageController: PageController(initialPage: initialIndex),
            enableRotation: true,
            // 允许旋转
            loadingBuilder: (context, event) => Center(
              child: CircularProgressIndicator(
                value: event == null
                    ? 0
                    : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
              ),
            ),
          ),
        ),
      ),
    );
  }

  // 链接步骤
  Widget _buildLinkStep(TaskStepModel step) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 文字描述
        if (step.taskDescription.isNotEmpty)
          Text(
            step.taskDescription,
            maxLines: 5,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(fontSize: 12, color: Color(0xFF666666)),
          ),

        // 链接预览
        if (step.taskLinks != null && step.taskLinks!.isNotEmpty)
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(height: 8),
              const Text(
                '链接:',
                style: TextStyle(fontSize: 12, color: Color(0xFF999999)),
              ),
              const SizedBox(height: 4),
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: const Color(0xFFF5F7FA),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  children: [
                    const Icon(Icons.link, size: 16, color: Color(0xFF666666)),
                    const SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        step.taskLinks!,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(
                          fontSize: 12,
                          color: Color(0xFF666666),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
            ],
          ),
      ],
    );
  }

  // 复制信息步骤
  Widget _buildInfoStep(TaskStepModel step) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 文字描述
        if (step.taskDescription.isNotEmpty)
          Text(
            step.taskDescription,
            maxLines: 5,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(fontSize: 12, color: Color(0xFF666666)),
          ),

        // 复制信息预览
        if (step.taskCopyInfo != null && step.taskCopyInfo!.isNotEmpty)
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(height: 8),
              const Text(
                '复制内容:',
                style: TextStyle(fontSize: 12, color: Color(0xFF999999)),
              ),
              const SizedBox(height: 4),
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: const Color(0xFFF5F7FA),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  children: [
                    const Icon(
                      Icons.content_copy,
                      size: 16,
                      color: Color(0xFF666666),
                    ),
                    const SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        step.taskCopyInfo!,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(
                          fontSize: 12,
                          color: Color(0xFF666666),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 8),
            ],
          ),
      ],
    );
  }

  // 默认步骤显示
  Widget _buildDefaultStep(TaskStepModel step) {
    return Text(
      step.taskDescription.isNotEmpty ? step.taskDescription : '未设置步骤内容',
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: const TextStyle(fontSize: 12, color: Color(0xFF666666)),
    );
  }

  Widget _buildTaskRequirementsSection(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '任务要求（必填）',
            style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
          ),
          const SizedBox(height: 8),
          GestureDetector(
            onTap: () => _editRequirements(viewModel),
            child: Container(
              padding: const EdgeInsets.all(12),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(4),
                border: Border.all(color: const Color(0xFFDDDDDD)),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Expanded(
                    child: Column(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            viewModel.taskRequirements ??
                                '请详细描述提交任务结果的要求，另可最多添加4张图片详细说明',
                            style: TextStyle(
                              fontSize: 14,
                              color: viewModel.taskRequirements != null
                                  ? const Color(0xFF333333)
                                  : const Color(0xFF999999),
                            ),
                            textAlign: TextAlign.left, // 额外设置文本对齐
                          ),
                        ),
                        if (viewModel.taskRequirementImages != null &&
                            viewModel.taskRequirementImages!.isNotEmpty)
                          SizedBox(
                            height: 96.h,
                            child: ListView.builder(
                              scrollDirection: Axis.horizontal,
                              itemCount: viewModel.taskRequirementImages.length,
                              padding: const EdgeInsets.only(bottom: 8),
                              itemBuilder: (context, imgIndex) {
                                return GestureDetector(
                                  onTap: () {
                                    _showImagePreview(
                                      context,
                                      viewModel.taskRequirementImages,
                                      imgIndex,
                                    );
                                  },
                                  child: Padding(
                                    padding: EdgeInsets.only(
                                      top: 8.h,
                                      right: 8.w,
                                    ),
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(8.r),
                                      child: CachedNetworkImage(
                                        imageUrl: viewModel
                                            .taskRequirementImages[imgIndex],
                                        height: 80.h,
                                        width: 80.h,
                                        fit: BoxFit.cover,
                                        errorWidget: (context, url, error) {
                                          return Image.asset(
                                            'assets/images/error_image_load.png',
                                            width: 80.h,
                                            height: 80.h,
                                            fit: BoxFit.cover,
                                          );
                                        },
                                      ),
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),
                      ],
                    ),
                  ),
                  const Icon(
                    Icons.arrow_forward_ios,
                    size: 12,
                    color: Color(0xFF999999),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildTaskRewardSection(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '任务奖励',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 16),

          // 单价
          _buildRewardRow(
            label: '任务奖励单价',
            child: SizedBox(
              width: 120.w,
              child: TextField(
                controller: _priceController,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: '请输入奖励单价',
                  hintStyle: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF999999),
                  ),
                  suffixText: '元',
                  suffixStyle: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF333333),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(4),
                    borderSide: const BorderSide(
                      color: Color(0xFFDDDDDD),
                    ), // 这里设置颜色
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(4),
                    borderSide: const BorderSide(color: Colors.blue), // 聚焦时的颜色
                  ),
                  contentPadding: const EdgeInsets.all(12),
                  filled: true,
                  fillColor: Colors.white,
                ),
                onChanged: (value) => _viewModel.setTitleChanged(value),
              ),
            ),
          ),
          const SizedBox(height: 8),

          // 服务费提示
          Padding(
            padding: const EdgeInsets.only(left: 0),
            child: Row(
              children: [
                Text(
                  '单价包含${widget.serviceFeePercent ?? viewModel.serviceFeePercent}%服务费',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                ),
                const SizedBox(width: 4),
                GestureDetector(
                  onTap: _showServiceFeeInfo,
                  child: const Icon(
                    Icons.help_outline,
                    size: 18,
                    color: Colors.blue,
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 16),

          // 次数
          _buildRewardRow(
            label: '任务预计奖励次数',
            child: SizedBox(
              width: 120.w,
              child: TextField(
                controller: _countController,
                keyboardType: TextInputType.number,
                decoration: InputDecoration(
                  hintText: '请输入任务总次数',
                  hintStyle: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF999999),
                  ),
                  suffixText: '次',
                  suffixStyle: const TextStyle(
                    fontSize: 14,
                    color: Color(0xFF333333),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(4),
                    borderSide: const BorderSide(
                      color: Color(0xFFDDDDDD),
                    ), // 这里设置颜色
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(4),
                    borderSide: const BorderSide(color: Colors.blue), // 聚焦时的颜色
                  ),
                  contentPadding: const EdgeInsets.all(12),
                  filled: true,
                  fillColor: Colors.white,
                ),
                onChanged: (value) => _viewModel.setTitleChanged(value),
              ),
            ),
          ),
          const SizedBox(height: 16),

          // 每人可完成次数
          _buildRewardRow(
            label: '每人可完成次数',
            child: GestureDetector(
              onTap: () => _showBidTimesOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 46.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        viewModel.bidTimesOptions?.firstWhere(
                              (option) => option.optionValue == viewModel.bidTimesPerUser,
                          orElse: () => TaskSelectOptionsItemModel(
                            optionValue: '',
                            optionDisplay: '请选择',
                          ),
                        ).optionDisplay ?? '请选择',
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
          const SizedBox(height: 16),

          // 奖励发放需发布人审核
          _buildRewardRow(
            label: '奖励发放需发布人审核',
            child: GestureDetector(
              onTap: () => _showReviewOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 46.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        viewModel.requireOwnerApproval,
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildRewardRow({required String label, required Widget child}) {
    return Row(
      children: [
        SizedBox(
          width: 140.h,
          child: Text(
            label,
            style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
          ),
        ),
        const SizedBox(width: 16),
        Expanded(child: child),
      ],
    );
  }

  Widget _buildOtherSettingsSection(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color(0xFFF5F7FA),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            '其他设置',
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Color(0xFF333333),
            ),
          ),
          const SizedBox(height: 16),

          // 设备类型 - 修改为选择方式
          _buildRewardRow(
            label: '支持设备（安卓/苹果）',
            child: GestureDetector(
              onTap: () => _showDeviceOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 44.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        _getDeviceDisplayName(viewModel.deviceType),
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
          const SizedBox(height: 16),

          // 审核时间 - 修改为选择方式
          _buildRewardRow(
            label: '雇主审核时间限制',
            child: GestureDetector(
              onTap: () => _showApproveDurationOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 44.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        viewModel.approveDurationOptions
                            ?.firstWhere(
                              (option) => option.optionValue == viewModel.approveDuration,
                          orElse: () => TaskSelectOptionsItemModel(
                            optionValue: '',
                            optionDisplay: '请选择',
                          ),
                        )
                            .optionDisplay ??
                            '请选择',
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
          // 服务费提示
          Padding(
            padding: const EdgeInsets.only(left: 0),
            child: Row(
              children: [
                Text(
                  '超过时间未审核将自动发放',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                )
              ],
            ),
          ),
          const SizedBox(height: 16),

          // 提交时间 - 修改为选择方式
          _buildRewardRow(
            label: '提交任务结果时间限制',
            child: GestureDetector(
              onTap: () => _showSubmitDurationOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 44.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        viewModel.submitDurationOptions
                            ?.firstWhere(
                              (option) => option.optionValue == viewModel.submitDuration,
                          orElse: () => TaskSelectOptionsItemModel(
                            optionValue: '',
                            optionDisplay: '请选择',
                          ),
                        )
                            .optionDisplay ??
                            '请选择',
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 0),
            child: Row(
              children: [
                Text(
                  '超过时间未提交任务结果，视为任务失败',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                )
              ],
            ),
          ),
          const SizedBox(height: 16),

          // 任务持续时间 - 修改为选择方式
          _buildRewardRow(
            label: '持续天数（自任务上线开始计时）',
            child: GestureDetector(
              onTap: () => _showTaskDurationOptions(viewModel),
              child: Container(
                width: 120.w,
                height: 44.h,
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  border: Border.all(color: const Color(0xFFDDDDDD)),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                      child: Text(
                        viewModel.taskDurationOptions
                            ?.firstWhere(
                              (option) => option.optionValue == viewModel.taskDuration,
                          orElse: () => TaskSelectOptionsItemModel(
                            optionValue: '',
                            optionDisplay: '请选择',
                          ),
                        )
                            .optionDisplay ??
                            '请选择',
                        style: const TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const Icon(Icons.arrow_drop_down, size: 16, color: Color(0xFF999999)),
                  ],
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 0),
            child: Row(
              children: [
                Text(
                  '时间结束不可接受任务，已接受任务可提交任务结果',
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 获取设备类型显示名称
  String _getDeviceDisplayName(String? deviceType) {
    switch (deviceType) {
      case 'IOS':
        return '苹果';
      case 'ANDROID':
        return '安卓';
      default:
        return '全部';
    }
  }


  Widget _buildBottomBar(TaskCreateFillInViewModel viewModel) {
    return Container(
      padding: const EdgeInsets.only(left: 16, right: 16, bottom: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(top: BorderSide(color: Colors.grey[300]!)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              // 使用Checkbox替代Radio，更符合协议选择的习惯
              Checkbox(
                value: viewModel.agreedToTerms,
                onChanged: (value) {
                  _viewModel.setAgreedToTerms(value ?? false);
                },
                activeColor: Colors.blue,
              ),
              Expanded(
                child: GestureDetector(
                  onTap: () {
                    // 点击协议文字时跳转到协议页面
                    _showAgreement();
                  },
                  child: RichText(
                    text: TextSpan(
                      children: [
                        const TextSpan(
                          text: '我已阅读并同意',
                          style: TextStyle(fontSize: 14, color: Color(0xFF333333)),
                        ),
                        TextSpan(
                          text: '《线上任务发布协议》',
                          style: const TextStyle(
                            fontSize: 14,
                            color: Colors.blue,
                            decoration: TextDecoration.underline,
                          ),
                          recognizer: TapGestureRecognizer()
                            ..onTap = () {
                              // 点击协议链接时跳转到协议页面
                              _showAgreement();
                            },
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
          Row(
            children: [
              Expanded(
                child: ElevatedButton(
                  onPressed: viewModel.agreedToTerms ? _submitTask : null,
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                    backgroundColor: Colors.blue,
                  ),
                  child: Text(
                    widget.taskId != null && !widget.againCreate
                        ? '修改任务'
                        : '发布任务',
                    style: const TextStyle(fontSize: 16, color: Colors.white),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 各种操作方法
  void _onBackPressed(TaskCreateFillInViewModel viewModel) {
    if (viewModel.hasChanged) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: const Text('提示'),
          content: const Text('是否要放弃当前编辑？'),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                Navigator.pop(context);
              },
              child: const Text('放弃'),
            ),
          ],
        ),
      );
    } else {
      Navigator.pop(context);
    }
  }

  void _selectTaskType() async {
    // 跳转到选择任务类型页面
    final selectedCategory = await RouteUtils.pushForNamed(
      context,
      RoutePath.task_create_choose_type,
      arguments: {
        'taskCategoryStr': _currentTaskCategoryStr,
        'refreshHandle': true,
      },
    );
    if (selectedCategory != null &&
        selectedCategory != _currentTaskCategoryStr) {
      setState(() {
        _currentTaskCategoryStr = selectedCategory;
      });
      //更新 ViewModel 中的值
      _viewModel.setTaskCategoryStr(_currentTaskCategoryStr);
      _viewModel.requestContent();
    }
  }

  void _editDescription(TaskCreateFillInViewModel viewModel) {
    // 跳转到编辑描述页面
  }

  void _addTaskStep() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('选择步骤类型'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              leading: SizedBox(
                width: 34.h,
                height: 34.h,
                child: Image.asset(
                  'assets/images/renwu_tuwen_icon.png',
                  fit: BoxFit.cover,
                ),
              ),
              title: const Text('图文说明'),
              onTap: () => _createStep(1),
            ),
            ListTile(
              leading: SizedBox(
                width: 34.h,
                height: 28.h,
                child: Image.asset(
                  'assets/images/renwu_wangzhi_icon.png',
                  fit: BoxFit.contain,
                ),
              ),
              title: const Text('网址链接'),
              onTap: () => _createStep(2),
            ),
            ListTile(
              leading: SizedBox(
                width: 34.h,
                height: 28.h,
                child: Image.asset(
                  'assets/images/renwu_fuzhi_icon.png',
                  fit: BoxFit.contain,
                ),
              ),
              title: const Text('复制信息'),
              onTap: () => _createStep(3),
            ),
          ],
        ),
      ),
    );
  }

  void _createStep(int type) async {
    Navigator.pop(context);
    if (type == 1) {
      TaskStepModel? model = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_image_text,
      );
      if (model != null) {
        _viewModel.addStep(model);
      }
    } else if (type == 2) {
      TaskStepModel? model = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_link,
      );
      if (model != null) {
        _viewModel.addStep(model);
      }
    } else if (type == 3) {
      TaskStepModel? model = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_copy,
      );
      if (model != null) {
        _viewModel.addStep(model);
      }
    }
  }

  void _editStep(TaskStepModel step) async {
    // 跳转到编辑步骤页面
    TaskStepModel? editedStep;

    // 根据步骤类型跳转到不同的编辑页面
    if (step.taskStepType == 'INFO') {
      editedStep = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_copy,
        arguments: step,
      );
    } else if (step.taskStepType == 'LINK') {
      editedStep = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_link,
        arguments: step,
      );
    } else {
      editedStep = await RouteUtils.pushForNamed(
        context,
        RoutePath.task_create_add_image_text,
        arguments: step,
      );
    }

    if (editedStep != null) {
      _viewModel.updateStep(editedStep);
    }
  }

  void _deleteStep(TaskStepModel step, TaskCreateFillInViewModel viewModel) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('确认删除'),
        content: const Text('确定要删除这个步骤吗？'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              viewModel.removeStep(step);
              Navigator.pop(context);
            },
            child: const Text('删除'),
          ),
        ],
      ),
    );
  }

  void _editRequirements(TaskCreateFillInViewModel viewModel) async {
    // 跳转到编辑任务要求页面
    TaskCreateFillInViewModel? model = await RouteUtils.pushForNamed(
      context,
      RoutePath.task_create_add_requirement,
      arguments: viewModel,
    );
    if (model != null) {
      _viewModel.setTaskRequirementImages(model.taskRequirementImages);
      _viewModel.setTaskRequirement(model.taskRequirements ?? '');
    }
  }

  void _showServiceFeeInfo() {
    // 显示服务费说明
    RouteUtils.pushForNamed(
      context,
      RoutePath.web_view,
      arguments: {'url': Constants.Fuwufei_Protocol},
    );
  }

  void _showAgreement() {
    // 显示协议内容
    RouteUtils.pushForNamed(
      context,
      RoutePath.web_view,
      arguments: {'url': Constants.Fabu_Protocol},
    );
  }

  // 每人可完成次数选项
  void _showBidTimesOptions(TaskCreateFillInViewModel viewModel) {
    if (viewModel.bidTimesOptions == null || viewModel.bidTimesOptions!.isEmpty) {
      return;
    }

    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择每人可完成次数',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.bidTimesOptions!.map((option) => ListTile(
              title: Text(
                option.optionDisplay,
                style: TextStyle(
                  fontSize: 16,
                  color: option.optionValue == viewModel.bidTimesPerUser
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option.optionValue == viewModel.bidTimesPerUser
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setBidTimesPerUser(option.optionValue);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

  // 奖励发放发布人审核选项
  void _showReviewOptions(TaskCreateFillInViewModel viewModel) {
    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择奖励是否需要发布人审核',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.guzhushenheOptionsDisplay.map((option) => ListTile(
              title: Text(
                option,
                style: TextStyle(
                  fontSize: 16,
                  color: option == viewModel.requireOwnerApproval
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option == viewModel.requireOwnerApproval
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setRequireOwnerApproval(option);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

  void _showDeviceOptions(TaskCreateFillInViewModel viewModel) {
    // 显示设备选项
    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择任务在哪些设备显示',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.deviceOptions!.map((option) => ListTile(
              title: Text(
                option.optionDisplay,
                style: TextStyle(
                  fontSize: 16,
                  color: option.optionValue == viewModel.deviceType
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option.optionValue == viewModel.deviceType
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setDeviceType(option.optionValue);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

  // 审核时间选项
  void _showApproveDurationOptions(TaskCreateFillInViewModel viewModel) {
    if (viewModel.approveDurationOptions == null || viewModel.approveDurationOptions!.isEmpty) {
      return;
    }

    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择雇主审核时间限制',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.approveDurationOptions!.map((option) => ListTile(
              title: Text(
                option.optionDisplay,
                style: TextStyle(
                  fontSize: 16,
                  color: option.optionValue == viewModel.approveDuration
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option.optionValue == viewModel.approveDuration
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setApproveDuration(option.optionValue);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

// 提交时间选项
  void _showSubmitDurationOptions(TaskCreateFillInViewModel viewModel) {
    if (viewModel.submitDurationOptions == null || viewModel.submitDurationOptions!.isEmpty) {
      return;
    }

    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择提交任务结果时间限制',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.submitDurationOptions!.map((option) => ListTile(
              title: Text(
                option.optionDisplay,
                style: TextStyle(
                  fontSize: 16,
                  color: option.optionValue == viewModel.submitDuration
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option.optionValue == viewModel.submitDuration
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setSubmitDuration(option.optionValue);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

// 任务持续时间选项
  void _showTaskDurationOptions(TaskCreateFillInViewModel viewModel) {
    if (viewModel.taskDurationOptions == null || viewModel.taskDurationOptions!.isEmpty) {
      return;
    }

    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              '选择任务持续时间',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            ...viewModel.taskDurationOptions!.map((option) => ListTile(
              title: Text(
                option.optionDisplay,
                style: TextStyle(
                  fontSize: 16,
                  color: option.optionValue == viewModel.taskDuration
                      ? Colors.blue
                      : Colors.black,
                ),
              ),
              trailing: option.optionValue == viewModel.taskDuration
                  ? const Icon(Icons.check, color: Colors.blue)
                  : null,
              onTap: () {
                _viewModel.setTaskDuration(option.optionValue);
                Navigator.pop(context);
              },
            )).toList(),
          ],
        ),
      ),
    );
  }

  void _previewTask() {
    // 跳转到预览页面
  }

  void _submitTask() {
    // 提交任务
    if (_validateForm()) {
      _viewModel.submitTask(
        title: _titleController.text,
        explain: _explainController.text,
        price: _priceController.text,
        count: _countController.text,
        onSuccess: (taskNo) {
          RouteUtils.pushForNamed(context, RoutePath.task_create_success, arguments: {'taskCategoryStr': widget.taskCategoryStr, 'taskId': taskNo});
        },
        onError: (error) {
          ScaffoldMessenger.of(
            context,
          ).showSnackBar(SnackBar(content: Text('发布失败: $error')));
        },
      );
    }
  }

  bool _validateForm() {
    if (_titleController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入任务标题')));
      return false;
    }
    if (_explainController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入任务说明')));
      return false;
    }
    if (_viewModel.taskSteps.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请添加任务步骤')));
      return false;
    }
    if (_viewModel.taskRequirements == null) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入任务要求')));
      return false;
    }
    if (_priceController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入任务奖励单价')));
      return false;
    }
    if (_countController.text.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请输入任务奖励次数')));
      return false;
    }
    return true;
  }

  @override
  void dispose() {
    _titleController.dispose();
    _explainController.dispose();
    _priceController.dispose();
    _countController.dispose();
    super.dispose();
  }
}
